package com.zhiyou100.blog.aciton.user;


import com.zhiyou100.blog.dao.UserDao;
import com.zhiyou100.blog.dao.impl.UserDaoImpl;
import com.zhiyou100.blog.entity.User;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
作者：苗天宝
班级：java37
班训：天道酬勤 不忘初衷
时间：2021年10月22日 下午3:44:35
*/
@WebServlet("/user/update")
public class UserUpdateServlet extends HttpServlet {

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletFileUpload servletUpload = new ServletFileUpload(new DiskFileItemFactory());
        List<FileItem> list = null;
        Map<String, String> map = new HashMap<String, String>();
        User loginUser = (User) req.getSession().getAttribute("loginUser");
        try {
            list = servletUpload.parseRequest(req);
            for (FileItem fileItem : list) {
                if (fileItem.isFormField()) {
                    map.put(fileItem.getFieldName(), fileItem.getString("UTF-8"));
                } else {
                    //获取img的路径
                    String path = req.getServletContext().getRealPath("/img");
                    String fileName = fileItem.getName();
                    File muDi = new File(path, System.currentTimeMillis() + fileName);
                    FileOutputStream fout = new FileOutputStream(muDi);
                    IOUtils.copy(fileItem.getInputStream(), fout);
                    fout.close();
                    map.put(fileItem.getFieldName(), muDi.getName());
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        UserDaoImpl userDao = new UserDaoImpl();

        //判断密码是不是和重复密码一致
        String message = null;
        if (!map.get("upwd").equals(map.get("reupwd"))) {
            message = "重复密码错误!";
        } else {
            User dbUser = userDao.getOneByName(map.get("uname"));
            if (dbUser != null && dbUser.getUid() != loginUser.getUid()) {
                message = "账号已存在！";
            }
        }
        if (message != null) {
            req.setAttribute("message", message);
            req.getRequestDispatcher("/index.jsp?pageName=user_regist.jsp").forward(req, resp);
            return;
        }
        User user = new User(
                Integer.parseInt(map.get("uid")),
                map.get("uname"),
                map.get("upwd"),
                map.get("usex"),
                new Date(),
                map.get("uphoto"),
                loginUser.getUscore(),
                loginUser.getUstatus());
        userDao.updateUser(user);
        req.setAttribute("message", "修改成功！");
        req.getSession().invalidate();//清空session
        req.getRequestDispatcher("/index.jsp").forward(req, resp);
    }

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
